package com.hehui.module.customer.controller;

import com.hehui.common.core.commom.AjaxResult;
import com.hehui.common.core.controller.BaseController;
import com.hehui.common.core.page.TableDataInfo;
import com.hehui.common.utils.SecurityUtils;
import com.hehui.common.utils.StringUtils;
import com.hehui.module.customer.entity.CustomerType;
import com.hehui.module.customer.service.ICustomerTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/customertype")
public class CustomerTypeController extends BaseController {

    @Autowired
    private ICustomerTypeService customerTypeService;


    /*查询客户类型列表
     */
    @GetMapping("/list")
    public TableDataInfo customerTypelist(CustomerType customerType) {
        List<CustomerType> list = customerTypeService.selectCustomerType(customerType);
        return getDataTable(list);
    }


    /*
     *根据客户类型ID 查询类型详细信息
     */
    @GetMapping(value = {"/", "/{customerTypeId}"})
    public AjaxResult getCustomerTypeInfoById(@PathVariable(value = "customerTypeId", required = false) Long customerTypeId) {
        AjaxResult ajax = AjaxResult.success();
        CustomerType type = customerTypeService.selectByPrimaryKey(customerTypeId);
        ajax.put("customerType", type);
        return ajax;
    }

    /*
     * 添加用户类型分组
     */
    @PostMapping("/add")
    public AjaxResult addCustomerType(@Validated @RequestBody CustomerType customerType) {

        //判断该商户下是否存在该用户类型
        if (StringUtils.isNotEmpty(customerType.getTypeName())) {
            if (customerTypeService.checkCustomerTypeNameUnique(customerType.getTypeName(), customerType.getMerchantId())) {
                return AjaxResult.error(StringUtils.format("添加({})失败，已有该类型", customerType.getTypeName()));
            }
            if (customerTypeService.checkCustomerTypeNumUnique(customerType.getTypeName(), customerType.getMerchantId())) {
                return AjaxResult.error(StringUtils.format("添加({})失败，已有该编号", customerType.getTypeName()));
            }
            return toAjax(customerTypeService.insertCustomerType(customerType));
        }
        return AjaxResult.error("添加失败,用户类型为空");
    }

    /*
     * 修改用户类型分组
     */
    @PostMapping("/update")
    public AjaxResult updateCustomerType(@Validated @RequestBody CustomerType customerType) {
        CustomerType old = customerTypeService.selectByPrimaryKey(customerType.getTypeId());

        if (StringUtils.isNotEmpty(customerType.getTypeName())) {
            //如果名字要修改的话，需要判断新名字是否存在于数据库中
            if (!old.getTypeName().equals(customerType.getTypeName())) {
                //类型名唯一性效验
                if (customerTypeService.checkCustomerTypeNameUnique(customerType.getTypeName(), customerType.getMerchantId())) {
                    return AjaxResult.error(StringUtils.format("修改({})失败，已有该类型", customerType.getTypeName()));
                }
            }
            if (!old.getTypeNum().equals(customerType.getTypeNum())) {
                if (customerTypeService.checkCustomerTypeNumUnique(customerType.getTypeName(), customerType.getMerchantId())) {
                    return AjaxResult.error(StringUtils.format("修改({})失败，已有该编号", customerType.getTypeName()));
                }
            }
            // 没有对 默认类型改为非默认类型做判断。。。 可以通过前端控制实现

            return toAjax(customerTypeService.updateByPrimaryKeySelective(customerType));
        }

        return AjaxResult.error("修改失败，已有该类型");

    }


    /*
     * 删除客户类型信息
     */
    @DeleteMapping(value = {"/", "/{customerTypeId}"})
    public AjaxResult deleteCustomer(@PathVariable(value = "customerTypeId", required = false) Long customerTypeId) {
        if (customerTypeId != 0L && customerTypeService.checkHasCustomerByTypeId(customerTypeId) == false) {
            return AjaxResult.error("删除失败，该用户类型下有用户");
        }

        return toAjax(customerTypeService.deleteCustomerTypeById(customerTypeId));
    }

    //通过商户ID查询设置编号
    @GetMapping("/getnum")
    public AjaxResult createLocaleNum() {
        return AjaxResult.success("成功",customerTypeService.createNewNum(SecurityUtils.getMerchantId()));
    }
}
